// -----------------------------------------------------------------------
//  <copyright file="AsyncEnumerableExtensions.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2022 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2022-12-23 1:22</last-date>
// -----------------------------------------------------------------------

namespace DaprPlus.Extensions;

public static class AsyncEnumerableExtensions
{
    /// <summary>
    /// 异步转List
    /// </summary>
    public static Task<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
    {
        if (source == null)
        {
            throw new ArgumentNullException(nameof(source));
        }

        return ExecuteAsync();

        async Task<List<T>> ExecuteAsync()
        {
            var list = new List<T>();
            await foreach (var item in source)
            {
                list.Add(item);
            }

            return list;
        }
    }
}
